using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using WindowsLive.Writer.Api;
using Delicious;

namespace DeliciousPlugin
{
    [WriterPlugin("7e7a08a3-035e-4a41-86b3-37801046a196", "Delicious Plugin",
        PublisherUrl = "http://scottisafooldev.spaces.live.com",
        Description = " ",
        ImagePath = "writer.png",
        HasEditableOptions = true)]

    public class Plugin : PublishNotificationHook
    {
        PluginSettings m_settings;
        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            m_settings = new PluginSettings(pluginOptions);
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            frmOptions op = new frmOptions(m_settings);
            op.ShowDialog();
        }

        public override void OnPostPublish(IWin32Window dialogOwner, IProperties properties, IPublishingContext publishingContext, bool publish)
        {
            if (!publish)
                return;

            // Protect against the possibility of blog services that don't return permalinks
            if (string.IsNullOrEmpty(publishingContext.PostInfo.Permalink))
            {
                PluginDiagnostics.LogError("DeliciousPlugin didn't execute, due to blank permalink");
                return;
            }
            if (!m_settings.AutomaticallyPost)
            {
                DialogResult result = MessageBox.Show("Would you like this post to be pushed to delicious?", "I need some info boss...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.No)
                    return;
            }

            string uname = m_settings.UserName;
            string pword = m_settings.Password;
            if (uname.Length == 0 || pword.Length == 0)
            {
                MessageBox.Show("No credentials have been set up, opening options...", "I need help boss", MessageBoxButtons.OK, MessageBoxIcon.Information);
                frmOptions op = new frmOptions(m_settings);
                DialogResult options = op.ShowDialog();
                if (options != DialogResult.OK)
                    return;
                else
                {
                    uname = m_settings.UserName;
                    pword = m_settings.Password;
                }
            }
            Delicious.Connection.Username = uname;
            Delicious.Connection.Password = pword;
            string categories = "";
            foreach (ICategoryInfo info in publishingContext.PostInfo.Categories)
            {
                categories += info.Name + " ";
            }
            Post.Add(publishingContext.PostInfo.Permalink, publishingContext.PostInfo.Title, GetContent(publishingContext.PostInfo.Contents), categories, DateTime.Now.ToShortTimeString());
            
        }

        private string GetContent(string p)
        {
            string s = Regex.Replace(p, @"<(.|\n)*?>", string.Empty);
            string result = "";
            if (p.Length > 200)
            {
                result = s.Substring(0, 200) + "...";
            }
            else
            {
                result = s;
            }
            return result;
        }




    }
}
